ggmap

Jan-Philipp Kolb

04/13/2015

Outline

Road maps

A road map is one of the most widely used map types.

Road maps

Install the library

install.packages("ggmap")

pic

pic

Library ggmap

library(ggmap)
qmap("Mannheim")

Map for a sight

qmap("Berlin Brandenburger Tor")

Map for a whole country

qmap("Germany")

Use another zoom level

qmap("Germany", zoom = 6)

Get help with the questionmark

?qmap

Different components in the help

The examples section of help

pic ```

Other zoom level

qmap("Mannheim", zoom = 12)

Get closer

qmap(location = 'Mannheim', zoom = 13)

Get very close

qmap('Mannheim', zoom = 20)

ggmap - source OpenStreetMap

qmap('Mannheim', zoom = 14, source="osm")

ggmap - OpenStreetMap - black and white

qmap('Mannheim', zoom = 14, source="osm",color="bw")

ggmap - maptype

qmap('Mannheim', zoom = 14, maptype="satellite")

ggmap - maptype

qmap('Mannheim', zoom = 14, maptype="hybrid")

ggmap - terrain map

qmap('Schriesheim', zoom = 14,
 maptype="terrain")

ggmap - maptype

qmap('Mannheim', zoom = 14,
 maptype="toner",source="stamen")

ggmap - maptype watercolor

qmap('Mannheim', zoom = 14,
 maptype="watercolor",source="stamen")

ggmap - maptype toner-lite

qmap('Mannheim', zoom = 14,
 maptype="toner-lite",source="stamen")

ggmap - maptype toner-hybrid

qmap('Mannheim', zoom = 14,
 maptype="toner-hybrid",source="stamen")

ggmap - maptype terrain-lines

qmap('Mannheim', zoom = 14,
 maptype="terrain-lines",source="stamen")

ggmap - create an object

MA_map <- qmap('Mannheim', 
               zoom = 14,
               maptype="toner",
               source="stamen")

Save graphics

pic

Geocoding

Geocoding (…) uses a description of a location, most typically a postal address or place name, to find geographic coordinates from spatial reference data …

Wikipedia - Geocoding

geocode("Mannheim Wasserturm",source="google")
lon lat
8.462233 49.48371

Reverse geocoding

Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name. This permits the identification of nearby street addresses, places, and/or areal subdivisions such as neighbourhoods, county, state, or country.

Source: Wikipedia

revgeocode(c(48,8))
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?latlng=8,48&sensor=false
## [1] "Qoriley Rd, Somalia"

Get the distance between 2 points

mapdist("Q1, 4 Mannheim","B2, 1 Mannheim")
##             from             to   m    km     miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 746 0.746 0.4635644     211 3.516667
##        hours
## 1 0.05861111
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="walking")
##             from             to   m    km     miles seconds minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 546 0.546 0.3392844     420       7
##       hours
## 1 0.1166667

Get another distance

mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="bicycling")
##             from             to   m    km    miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 555 0.555 0.344877     215 3.583333
##        hours
## 1 0.05972222

Geocoding - points of interest

POI1 <- geocode("B2, 1 Mannheim",source="google")
POI2 <- geocode("Hbf Mannheim",source="google")
POI3 <- geocode("Wasserturm Mannheim",source="google")
ListPOI <-rbind(POI1,POI2,POI3)
POI1;POI2;POI3
##        lon      lat
## 1 8.462844 49.48569
##        lon      lat
## 1 8.469879 49.47972
##        lon      lat
## 1 8.473664 49.48483

Points in map

MA_map +
geom_point(aes(x = lon, y = lat),
data = ListPOI)

Points in map

MA_map +
geom_point(aes(x = lon, y = lat),col="red",
data = ListPOI)

ggmap - adding different colors

ListPOI$color <- c("A","B","C")
MA_map +
geom_point(aes(x = lon, y = lat,col=color),
data = ListPOI)

ggmap - bigger dots

ListPOI$size <- c(10,20,30)
MA_map +
geom_point(aes(x = lon, y = lat,col=color,size=size),
data = ListPOI)

Get a route from Google maps

from <- "Mannheim Hbf"
to <- "Mannheim B2 , 1"
route_df <- route(from, to, structure = "route")

More information

Draw a map with this information

qmap("Mannheim Hbf", zoom = 14) +
  geom_path(
    aes(x = lon, y = lat),  colour = "red", size = 1.5,
    data = route_df, lineend = "round"
  )

Resources

More about adding points

pic

Cheatsheet

pic

Resources and literature

ggmap: Spatial Visualization with ggplot2

by David Kahle and Hadley Wickham